package com.demo.autotest.controller;

import com.demo.autotest.common.utils.PageUtils;
import com.demo.autotest.database.domain.Project;
import com.demo.autotest.database.domain.User;
import com.demo.autotest.dto.PageDTO;
import com.demo.autotest.dto.UserDTO;
import com.demo.autotest.request.QueryRequest;
import com.demo.autotest.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController // 定义一个controller类
@RequestMapping("/autotest/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/save")
    public void saveUser(@RequestBody User user){
        userService.saveUser(user);
    }

    @PostMapping("/delete/{id}")
    public void deleteUser(@PathVariable String id){
        userService.deleteUser(id);
    }

    @PostMapping("/reset/pwd/{id}")
    public void resetPwd(@PathVariable String id){
        userService.resetPwd(id);
    }

    @GetMapping("/get/{id}")
    public User getUser(@PathVariable String id){
        return userService.getUserById(id);
    }

    @GetMapping("/project/list/{userId}")
    public List<Project> getUserProjectList(@PathVariable String userId){
        return userService.getUserProjectList(userId);
    }

    @GetMapping("/all/list")
    public List<User> getUserAllList(){
        return userService.getUserAllList();
    }

    @PostMapping("/list/{pageNum}/{pageSize}")
    public PageDTO<List<UserDTO>> getUserList(@PathVariable Integer pageNum,
                                              @PathVariable Integer pageSize,
                                              @RequestBody QueryRequest queryRequest,
                                              HttpServletRequest request){
        String user = request.getSession().getAttribute("userId").toString();
        queryRequest.setRequestUser(user);
        Page<Object> page = PageHelper.startPage(pageNum, pageSize, true);
        return PageUtils.setPageInfo(page, userService.getUserList(queryRequest));
    }
}
